package function;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import entity.Manager;
import interfaces.ManagerDao;

import java.sql.Connection;
import java.util.List;

public class ManagerDaoImpl extends BaseDao2<Manager> implements ManagerDao {

    private static ManagerDaoImpl managerDao=null;
    private ManagerDaoImpl() {
    }
    public static ManagerDaoImpl getManagerDao(){
        if(managerDao==null){
            managerDao=new ManagerDaoImpl();
        }
        return managerDao;
    }


    @Override
    public boolean insertOperator(Connection conn, Manager manager) {
        String sql="insert into manager(name,password) values(?,?)";
        int update = 0;
        try {
            update = update(conn, sql, manager.getName(), manager.getPassword());
        } catch (Exception e) {
            System.out.println("用户名已存在");
        }
        if(update==0)return false;
        else return true;
    }

    @Override
    public boolean deleteOperator(Connection conn, int id) {
       String sql="delete from manager where id=?";
        int update = 0;
        try {
            update = update(conn, sql, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(update==0)return false;
        else return true;
    }

    @Override
    public boolean updateOperator(Connection conn, Manager manager)  {
       String sql="update manager set name=?,password=? where id=?";
        int update = 0;
        try {
            update = update(conn, sql, manager.getName(), manager.getPassword(), manager.getId());
        } catch (MySQLIntegrityConstraintViolationException throwables) {
            System.out.println("该用户名以存在");
        }
        if(update==0)
        {
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public Manager selectById(Connection conn, int id) {
        String sql="select id id,name name,password password from manager where id=?";
        Manager instance = null;
        try {
            instance = getInstance(conn, sql, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }

    @Override
    public Manager selectByName(Connection conn, String name) {
        String sql="select id id,name name,password password from manager where name=?";
        Manager instance = null;
        try {
            instance = getInstance(conn, sql, name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }

    @Override
    public List<Manager> selectAll(Connection conn) {
        String sql="select id id,name name,password password from manager";
        List<Manager> forList = null;
        try {
            forList = getForList(conn, sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forList;
    }

    @Override
    public Manager check(Connection conn, String name, String password) {
        String sql="select id id,name name,password password from manager where name=? and password=?";
        Manager m = getInstance(conn, sql, name, password);
      return m;
    }
}
